<?php

class Auth_Acl {
	/**
	* @param Zend_Config|array|string $smth
	* @return Auth_Acl_StorageDecorator
	*/
	static function factory($smth = null) {

		$cache = false;
		$storageClassnamePrefix = 'Auth_Acl_Storage_';

		if ($smth instanceof Zend_Config) {
			$smth = $smth->toArray();
		}

		
		if (is_array($smth)) {
			$tmp = $smth;
			$smth = isset($tmp['storage']) ? $tmp['storage'] : null;
			$cache = isset($tmp['useCache']) ?$tmp['useCache'] : false;		
			$storageClassnamePrefix =  isset($tmp['storagePrefix']) ? $tmp['storagePrefix'] : $storageClassnamePrefix;	
		}
		
		$result = new Auth_Acl_StorageDecorator(new Zend_Acl);
		
		if (is_string($smth) && trim($smth) != '') {
			
			$storageName = $storageClassnamePrefix.ucfirst(strtolower($smth));
			
			if (!class_exists($storageName)) throw new Auth_Acl_Exception ('Acl storage with name '.$storageName.' does not exists. ');
			else {
				$storage = new $storageName;
				$storage->useCache($cache);
				$result->setStorage($storage);
			}
		} elseif (is_object($smth)) {
			if ($smth instanceof Auth_Acl_Storage_Abstract) {
				$result->setStorage($smth);
			}
		}

		return $result;
	}
	 
}